package com.boarsoft.boar.batch.mq;

import com.alibaba.fastjson.JSONObject;
import com.boarsoft.common.util.RandomUtil;
import com.boarsoft.message.bean.Message;
import com.boarsoft.message.core.Messenger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

/**
 * SOFAMQ生产者,当某个批次处理完成时给运管发送消息通知
 */
public class MsgProducer {

    private static final Logger log = LoggerFactory.getLogger(MsgProducer.class);

    @Value("${sofa.mq.topic:TP_batch-dispatch-notice}")
    private String topic;

    /**
     * 消息发送
     *
     * @param jsonObject
     * @throws Exception
     */
    public void send(JSONObject jsonObject) throws Exception {
        String msgId = RandomUtil.randomUUID();
        log.info(" start notice YG ... {}, message id is {}", jsonObject, msgId);
        long start = System.currentTimeMillis();
        Message msg = new Message();
        msg.setId(msgId);
        msg.setTarget(topic);
        msg.setContent(jsonObject);
        msg.setTime(System.currentTimeMillis());
        Messenger.send(msg);
        log.info(" end notice YG ... spendTime {} ms", System.currentTimeMillis() - start);
    }
}
